package com.innoking.docsys.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.doc.module.UpmsFondsManageVO;
import com.innoking.docsys.report.feign.UserRestInterface;
import com.innoking.docsys.report.module.CompreClassVO;
import com.innoking.docsys.report.service.CollectionService;
import com.innoking.docsys.user.module.UpmsOrganizationVO;
import com.innoking.docsys.user.module.UpmsUserVO;
import com.innoking.docsys.utils.JsonToOtherUtil;
import com.innoking.docsys.utils.OtherPojo;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReportController
 * @Description 统计管理
 * @Author wr
 * @Date 2019/9/4 000415:49
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    Logger logger= LoggerFactory.getLogger(ReportController.class);

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private UserRestInterface userRestInterface;
  /**
     * 综合统计（按分类）
     * @param data
     * @return
     */
    @PostMapping("/compre_class")
    @ResponseBody
    public ResultObjVO compreClass(@RequestBody String data) {
        logger.info("*****综合统计（按分类）进入");
        logger.info("*****综合统计（按分类）入参：data="+data);
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String userId = jsonObject.get("userId").toString();
        if(StringUtils.isBlank(userId)){
            return ResultUtil.error("userId不能为空");
        }
        String fondsId = jsonObject.get("fondsId").toString();
        if(StringUtils.isBlank(fondsId)){
            //组织机构为空时，默认所有，选择当前用户所在机构
            UpmsUserVO upmsUserVOByUserId = getUpmsUserVOByUserId(userId);
            //该字段保存用户所在部门code
            BigDecimal deptId = upmsUserVOByUserId.getDeptId();
            if(deptId==null){
                return ResultUtil.error("用户所在机构code不能为空");
            }
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("orgCode",String.valueOf(deptId));
            String s1 = JsonToOtherUtil.packageEntering(stringObjectHashMap);
            String fondsByOrgCode = userRestInterface.getFondsByOrgCode(s1);
            ResultObjVO resultObjVO = JsonToOtherUtil.rtResultObjVO(fondsByOrgCode, new UpmsFondsManageVO());
            UpmsFondsManageVO obj = (UpmsFondsManageVO) resultObjVO.getObj();
            fondsId = obj.getFondsId().toString();
        }
        String startDate = jsonObject.get("startDate").toString();
        String endDate = jsonObject.get("endDate").toString();
        List<CompreClassVO> maps=null;
        if(StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)){
            //查询所有时间段
             maps = collectionService.compreClass(userId, fondsId, null, null);
        }else if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
            return ResultUtil.error("开始时间或结束时间为空");
        }else {
            //开始时间和结束时间都不为空时，按时间查询
             maps = collectionService.compreClass(userId, fondsId, startDate, endDate);
        }
        return ResultUtil.success( maps );
    }


    /**
     * 根据用户id获取当前用户信息
     * @param userId
     * @return
     */
    public UpmsUserVO getUpmsUserVOByUserId(String userId){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("userId",userId);
        String dataValue = JsonToOtherUtil.packageEntering(stringObjectHashMap);
        String userbyId = userRestInterface.getUserbyId(dataValue);
        ResultObjVO resultObjVO = JsonToOtherUtil.rtResultObjVO(userbyId, new UpmsUserVO());
        UpmsUserVO obj = (UpmsUserVO) resultObjVO.getObj();
        return obj;
    }

    /**
     * 根据组织机构code获取组织机构信息
     * @param orgCode
     * @return
     */
    public UpmsOrganizationVO getOrgVOByOrgCode(String orgCode){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("orgCode",orgCode);
        String dataValue = JsonToOtherUtil.packageEntering(stringObjectHashMap);
        String userbyId = userRestInterface.getOrgbyOrgCode(dataValue);
        ResultObjVO resultObjVO = JsonToOtherUtil.rtResultObjVO(userbyId, new UpmsOrganizationVO());
        UpmsOrganizationVO obj = (UpmsOrganizationVO) resultObjVO.getObj();
        return obj;
    }


}
